import { onMounted, onUnmounted } from 'vue'

const useKeyBoard = (map) => {
   let handleKeydown = function (e) {
    const callbackObj = map[e.code]
    if (callbackObj && callbackObj.keydown) callbackObj.keydown(e);
  }
   let handleKeyup = function (e) {
    const callbackObj = map[e.code]
    if (callbackObj && callbackObj.keyup) callbackObj.keyup(e);
  }

  onMounted(()=>{
    window.addEventListener('keydown', handleKeydown)
    window.addEventListener('keyup', handleKeyup)
  })

  onUnmounted(()=> {
    window.removeEventListener('keydown', handleKeydown)
    window.removeEventListener('keyup', handleKeyup)
  })

}

export default useKeyBoard